//
//  PersonalViewController.m
//  一键车讯
//
//  Created by Scott on 14/10/27.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "PersonalViewController.h"
#import "LoginViewController.h"
#import "MaintainViewController.h"
#import "WebColor.h"
#import <ShareSDK/ShareSDK.h>
#import <Parse/Parse.h>
#import "UIImageView+WebCache.h"

@interface PersonalViewController ()

@property (nonatomic, retain) UIImageView *IconImage;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *editButton;
@property (nonatomic, retain) UIButton *forumButton;
@property (nonatomic, retain) UIButton *mainTainButton;
@property (nonatomic, retain) UIButton *collectButton;
@property (nonatomic, retain) UIButton *settingButton;
@property (nonatomic, retain) UILabel *recentLabel;
@property (nonatomic, retain) UILabel *forumLabel;
@property (nonatomic, retain) UILabel *carLabel;
@property (nonatomic, retain) UIButton *buttonOne;
@property (nonatomic, retain) UIButton *buttonTwo;
@property (nonatomic, retain) UIButton *buttonThree;
@property (nonatomic, retain) UIButton *buttonFour;
@property (nonatomic, retain) UIButton *buttonFive;
@property (nonatomic, retain) UIButton *buttonSix;
@property (nonatomic, retain) UIButton *buttonSeven;
@property (nonatomic, retain) UIButton *buttonEight;
@property (nonatomic, retain) NSString *nickName;
@property (nonatomic, retain) NSString *profileImage;

@end

@implementation PersonalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.IconImage = [[UIImageView alloc] init];
        
    }
    return self;
}

- (void)dealloc
{
    [_IconImage release];
    [_recentLabel release];
    [_forumLabel release];
    [_carLabel release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{

    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 头像
    self.title = @"个人中心";
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSArray *pathProfileImage = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentImage = [pathProfileImage lastObject];
    NSString *pathImage = [documentImage stringByAppendingString:@"/profileImage.txt"];
    
    self.profileImage = [NSString stringWithContentsOfFile:pathImage encoding:NSUTF8StringEncoding error:nil];
    
//    NSLog(@"%@", self.profileImage);
    
    
    // 从本地读取显示头像
    
    if ([self.profileImage isEqualToString:@"none"]) {
        self.IconImage.image = [UIImage imageNamed:@"geren.png"];
    
    } else {
        
        [self.IconImage setImageWithURL:[NSURL URLWithString:self.profileImage]];
        
    }
    
    self.IconImage.backgroundColor = [UIColor whiteColor];
    self.IconImage.frame = CGRectMake(30, 90, 50, 50);
    [self.view addSubview:self.IconImage];
    [_IconImage release];
    
    
    // 点击登陆 按钮
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.loginButton.frame = CGRectMake(85, 90, 80, 30);
    self.loginButton.backgroundColor = [UIColor clearColor];
    
    NSArray *pathNickName = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPathNickName = [pathNickName lastObject];
    NSString *strPathNickName = [documentPathNickName stringByAppendingString:@"/nickName.txt"];
    
    self.nickName = [NSString stringWithContentsOfFile:strPathNickName encoding:NSUTF8StringEncoding error:nil];

    if ([self.nickName isEqualToString:@"none"]) {
        [self.loginButton setTitle:@"点击登录" forState:UIControlStateNormal];
        
    } else {
        
        [self.loginButton setTitle:self.nickName forState:UIControlStateNormal];
    }
    
    
    [self.loginButton addTarget:self action:@selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    
    
    // 编辑资料
    self.editButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.editButton.frame = CGRectMake(160, 115, 120, 25);
    self.editButton.backgroundColor = [UIColor cyanColor];
    [self.editButton setTitle:@"编辑资料" forState:UIControlStateNormal];
    [self.editButton addTarget:self action:@selector(editClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.editButton];
    [_editButton release];
    
    // 我的论坛
    self.forumButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.forumButton.frame = CGRectMake(30, 160, self.view.frame.size.width / 5, 40);
    self.forumButton.backgroundColor = [UIColor cyanColor];
    [self.forumButton setTitle:@"我的论坛" forState:UIControlStateNormal];
    [self.forumButton addTarget:self action:@selector(forumClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.forumButton];
    
    // 我的保养
    self.mainTainButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.mainTainButton.frame = CGRectMake(30 + self.view.frame.size.width / 5, 160, self.view.frame.size.width / 5, 40);
    self.mainTainButton.backgroundColor = [UIColor redColor];
    [self.mainTainButton setTitle:@"我的保养" forState:UIControlStateNormal];
    [self.mainTainButton addTarget:self action:@selector(mainTainClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.mainTainButton];
    
    
    // 我的收藏
    self.collectButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collectButton.frame = CGRectMake(30 + self.view.frame.size.width / 5 * 2, 160, self.view.frame.size.width / 5, 40);
    self.collectButton.backgroundColor = [UIColor grayColor];
    [self.collectButton setTitle:@"我的收藏" forState:UIControlStateNormal];
    [self.collectButton addTarget:self action:@selector(collectClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.collectButton];
    // 我的设置
    self.settingButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.settingButton.frame = CGRectMake(30 + self.view.frame.size.width / 5 * 3, 160, self.view.frame.size.width / 5, 40);
    self.settingButton.backgroundColor = [UIColor whiteColor];
    [self.settingButton setTitle:@"我的设置" forState:UIControlStateNormal];
    [self.settingButton addTarget:self action:@selector(settingClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.settingButton];
    
    // 最近访问
    self.recentLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 220, 60, 40)];
    self.recentLabel.backgroundColor = [UIColor blueColor];
    [self.recentLabel setText:@"最近访问"];
    self.recentLabel.font = [UIFont systemFontOfSize:14];
    [self.recentLabel sizeToFit];
    [self.view addSubview:self.recentLabel];
    
    // 论坛
    self.forumLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 270, 40, 40)];
    self.forumLabel.backgroundColor = [WebColor pink];
    [self.forumLabel setText:@"论坛"];
    self.forumLabel.font = [UIFont systemFontOfSize:14];
    self.forumLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.forumLabel];
    
    
    // 读取沙盒
    NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document1 = [pathArr lastObject];
    NSString *strPath1 = [document1 stringByAppendingString:@"/arr.xml"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:strPath1];
    
    NSLog(@"%@", arr);
    
    //按钮1
    // 论坛1
    self.buttonOne = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonOne.frame = CGRectMake(80, 270, 50, 40);
    self.buttonOne.backgroundColor = [UIColor whiteColor];
    self.buttonOne.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonOne.titleLabel.numberOfLines = 0;

    NSLog(@"%@", arr[0]);
    
    if (arr.count > 0) {
        [self.buttonOne setTitle:arr[0] forState:UIControlStateNormal];
        NSLog(@"%@", self.buttonOne.titleLabel.text);
    }
    
    
//    [self.buttonOne addTarget:self action:@selector(buttonOneClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonOne];
    
    
    //按钮2
    // 论坛2
    self.buttonTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonTwo.frame = CGRectMake(140, 270, 50, 40);
    self.buttonTwo.backgroundColor = [UIColor whiteColor];
    self.buttonTwo.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonTwo.titleLabel.numberOfLines = 0;

    if (arr.count > 1) {
        [self.buttonTwo setTitle:arr[1] forState:UIControlStateNormal];
    
    }
    
    //    [self.buttonTwo addTarget:self action:@selector(buttonTwoClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonTwo];
    
    
    //按钮3
    // 论坛3
    self.buttonThree = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonThree.frame = CGRectMake(200, 270, 50, 40);
    self.buttonThree.backgroundColor = [UIColor whiteColor];
    self.buttonThree.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonThree.titleLabel.numberOfLines = 0;

    if (arr.count > 2) {
        [self.buttonThree setTitle:arr[2] forState:UIControlStateNormal];
    }
    
    //    [self.buttonThree addTarget:self action:@selector(buttonOneClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonThree];
   
    
    //按钮4
    // 论坛4
    self.buttonFour = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonFour.frame = CGRectMake(260, 270, 50, 40);
    self.buttonFour.backgroundColor = [UIColor whiteColor];
    self.buttonFour.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonFour.titleLabel.numberOfLines = 0;

    if (arr.count > 3) {
        [self.buttonFour setTitle:arr[3] forState:UIControlStateNormal];
    }
    

    //    [self.buttonFour addTarget:self action:@selector(buttonOneClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonFour];
    
    
    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [path lastObject];
    NSString *strPath = [document stringByAppendingString:@"/array.xml"];
    //    NSMutableArray *arr1 = [NSMutableArray array];
    //    [arr1 writeToFile:strPath atomically:YES];
    
//    NSLog(@"%@", strPath);
    
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:strPath];


    // 车系
    self.carLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 320, 40, 40)];
    self.carLabel.backgroundColor = [WebColor pink];
    [self.carLabel setText:@"车系"];
    self.carLabel.font = [UIFont systemFontOfSize:14];
    self.carLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.carLabel];
    
    //按钮5
    self.buttonFive = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonFive.frame = CGRectMake(80, 320, 50, 40);
    self.buttonFive.backgroundColor = [UIColor whiteColor];
    self.buttonFive.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonFive.titleLabel.numberOfLines = 0;
    
    
    if (array.count > 0) {
        
        [self.buttonFive setTitle:array[0] forState:UIControlStateNormal];

    }
    [self.buttonFive addTarget:self action:@selector(buttonFiveClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonFive];
    
    
    
    //按钮6
    self.buttonSix = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonSix.frame = CGRectMake(140, 320, 50, 40);
    self.buttonSix.backgroundColor = [UIColor whiteColor];
    self.buttonSix.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonSix.titleLabel.numberOfLines = 0;
    
    if (array.count > 1) {
        [self.buttonSix setTitle:array[1] forState:UIControlStateNormal];
        
    }
    
    [self.buttonSix addTarget:self action:@selector(buttonSixClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonSix];
    
    //按钮7
    self.buttonSeven = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonSeven.frame = CGRectMake(200, 320, 50, 40);
    self.buttonSeven.backgroundColor = [UIColor whiteColor];
    self.buttonSeven.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonSeven.titleLabel.numberOfLines = 0;
    
    if (array.count > 2) {
        [self.buttonSeven setTitle:array[2] forState:UIControlStateNormal];
        
    }
    [self.buttonSeven addTarget:self action:@selector(buttonSevenClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonSeven];
    //按钮8
    
    self.buttonEight = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonEight.frame = CGRectMake(260, 320, 50, 40);
    self.buttonEight.backgroundColor = [UIColor whiteColor];
    self.buttonEight.titleLabel.font = [UIFont systemFontOfSize:11];
    self.buttonEight.titleLabel.numberOfLines = 0;
    
    if (array.count > 3) {
        [self.buttonEight setTitle:array[3] forState:UIControlStateNormal];
    }
    
    [self.buttonEight addTarget:self action:@selector(buttonEightClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonEight];
    
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barLeftButtonItemAction:)] autorelease];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleBordered target:self action:@selector(logoutButtonClickHandler:)] autorelease];
    
    
}

//UINavigationItem方法
- (void)barLeftButtonItemAction:(UIBarButtonItem *)barButton
{
    // 返回首页
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

// 注销方法
- (void)logoutButtonClickHandler:(id)sender
{
    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentPath = [path lastObject];
    
    NSString *strPath = [documentPath stringByAppendingString:@"/nickName.txt"];
    
    NSString *strPathImage = [documentPath stringByAppendingString:@"/profileImage.txt"];
    
    NSString *str = @"none";
    
    NSString *strImage = @"none";
    
    [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    [strImage writeToFile:strPathImage atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
    [self.loginButton setTitle:@"点击登录" forState:UIControlStateNormal];
    
    self.IconImage.image = [UIImage imageNamed:@"geren.png"];

    
    [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
    
    [self.navigationController popViewControllerAnimated:NO];
}



// 点击登陆 按钮方法
- (void)loginClicked:(UIButton *)button
{
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo
                      authOptions:nil
                           result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
                               
                               
                               NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                               
                               NSString *documentPath = [path lastObject];
                               
                               NSString *strPath = [documentPath stringByAppendingString:@"/nickName.txt"];
                               
                               NSString *strPathImage = [documentPath stringByAppendingString:@"/profileImage.txt"];
                               
                               
                               [userInfo.nickname writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
                               
                               [userInfo.profileImage writeToFile:strPathImage atomically:YES encoding:NSUTF8StringEncoding error:nil];
//                               NSLog(@"%@", userInfo.profileImage);
                               
                               
                               // 将Button的名字改为博客的昵称
                               //
                               [self.loginButton setTitle:userInfo.nickname forState:UIControlStateNormal];
                               
                               // 显示新浪微博的头像
                               [self.IconImage setImageWithURL:[NSURL URLWithString:userInfo.profileImage]];
                               
                               
                               if (result)
                               {
                                   PFQuery *query = [PFQuery queryWithClassName:@"UserInfo"];
                                   [query whereKey:@"uid" equalTo:[userInfo uid]];
                                   [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                                       
                                       if ([objects count] == 0)
                                       {
                                           PFObject *newUser = [PFObject objectWithClassName:@"UserInfo"];
                                           [newUser setObject:[userInfo uid] forKey:@"uid"];
                                           [newUser setObject:[userInfo nickname] forKey:@"name"];
                                           [newUser setObject:[userInfo profileImage] forKey:@"icon"];
                                           [newUser saveInBackground];
                                           
                                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"欢迎注册" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
                                           [alertView show];
                                           [alertView release];
                                       }
                                       else
                                       {
                                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"欢迎回来" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
                                           [alertView show];
                                           [alertView release];
                                       }
                                   }];
                                   
//                                   MainViewController *mainVC = [[[MainViewController alloc] init] autorelease];
//                                   [self.navigationController pushViewController:mainVC animated:YES];
                                   
                               }
                               
                           }];
}



// 编辑资料方法
- (void)editClicked:(UIButton *)button
{
    NSLog(@"编辑资料");
    
}

// 我的论坛
- (void)forumClicked:(UIButton *)button
{
    NSLog(@"我的论坛");
}
// 我的保养
- (void)mainTainClicked:(UIButton *)button
{
    MaintainViewController *mait = [[MaintainViewController alloc] init];
    [self.navigationController pushViewController:mait animated:YES];
    
    [mait release];
    
    
}
// 我的收藏
- (void)collectClicked:(UIButton *)button
{
    NSLog(@"我的收藏");
}
// 我的设置
- (void)settingClicked:(UIButton *)button
{
    NSLog(@"我的设置");
}

// 按钮5
- (void)buttonFiveClicked:(UIButton *)button
{
    
}
// 按钮6
- (void)buttonSixClicked:(UIButton *)button
{
    
}
// 按钮7
- (void)buttonSevenClicked:(UIButton *)button
{
    
}
// 按钮8
- (void)buttonEightClicked:(UIButton *)button
{
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
